package cn.yyh.serviceOrg.controller;


import cn.yyh.commons.utils.PageInfo;
import cn.yyh.commons.utils.R;
import cn.yyh.serviceOrg.entity.Department;
import cn.yyh.serviceOrg.entity.Employee;
import cn.yyh.serviceOrg.query.DepartmentQuery;
import cn.yyh.serviceOrg.service.DepartmentService;
import cn.yyh.serviceOrg.service.EmployeeService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yang
 * @since 2021-07-10
 */
@RestController
@RequestMapping("/serviceOrg/dept")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private EmployeeService employeeService;

    @ApiOperation(value = "高级查询,并分页")
    @PostMapping("/pageQuery/{current}/{size}")
    public R pageQuery(
            @ApiParam(name = "current",value = "当前页",required = true)
            @PathVariable Integer current,
            @ApiParam(name = "size",value = "每页展示条数",required = true)
            @PathVariable Integer size,
            @ApiParam(name = "query",value = "高级查询")
            @RequestBody(required = false) DepartmentQuery query) {

        query.setCurrentPage(current);
        query.setPageSize(size);
        try {
            PageInfo<Department> pageInfo = departmentService.pageQuery(query);
            return R.oK().data("totals", pageInfo.getTotals()).data("rows",pageInfo.getRows());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "根据ID查询部门")
    @GetMapping("{id}")
    public R getById(
            @ApiParam(name = "id",value = "部门编号",required = true)
            @PathVariable Long id) {

        try {
            Department byId = departmentService.getById(id);
            return R.oK().data("rows", byId);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "根据有没有ID判断是添加还是修改部门")
    @PostMapping("save")
    public R saveOrUpdate(
            @ApiParam(name = "department",value = "部门数据封装对象")
            @RequestBody Department department) {

        try {
            if (department.getId() == null) {
                departmentService.save(department);
            }else {
                departmentService.update(department);
            }
            return R.oK();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "根据ID删除部门")
    @DeleteMapping("{id}")
    public R delete(
            @ApiParam(name = "id",value = "部门编号",required = true)
            @PathVariable Long id){

        try {
            departmentService.delete(id);
            return R.oK();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }


    @ApiOperation(value = "根据id批量删除部门")
    @PatchMapping("/deleteBatch")
    public R deleteBatch(
            @ApiParam(name = "ids",value = "部门编号集合",required = true)
            @RequestBody List<Long> ids){

        try {
            departmentService.deleteBatch(ids);
            return R.oK();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "查询所有部门和员工做回显")
    @GetMapping
    public R getAllDeptAndEmp() {
        try {
            List<Department> deptList = departmentService.listAll();
            List<Employee> empList = employeeService.listAll();
            return R.oK().data("dept", deptList).data("emp", empList);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "查询部门树,做上级部门回显")
    @GetMapping("tree/{id}")
    public R listDeptTree(@PathVariable Long id){
        try {
            List<Department> list = departmentService.listDeptTree(id);
            return R.oK().data("tree", list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }
}

